package com.vsct.maia.auditl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.vsct.maia.auditl.dto.ErrorsTableLine;
import com.vsct.maia.auditl.dto.MethodTableLine;
import com.vsct.maia.auditl.dto.ServiceExecTimes;
import com.vsct.maia.auditl.dto.ServiceTableLine;

public class TableBuilderJson {
	
	public String getChartDataJson( List<String> services,
									String dateDebut,
									String dateFin,
									String groupBy,
									String percentile){
		
		List<ServiceExecTimes>  finalRows= new ArrayList<ServiceExecTimes>();
    	StringBuilder serie = new StringBuilder();   
    	
    	DataManager str = new DataManager();
    	finalRows = str.getExecTimeByCriteria(services, dateDebut, dateFin,groupBy,percentile);
    	
    	boolean addComa = false;
    	serie.append("[");
    	for (ServiceExecTimes serviceExecTimes : finalRows) {
    		if(addComa)
    			serie.append(",");
    		serie.append("{");
    		serie.append(serviceExecTimes.toString());
    		serie.append("}");
    		addComa=true;
		}
    	serie.append("]");
    	return serie.toString();
	}
	
	public String getServiceTableJson(List<ServiceTableLine> tabService) {
		StringBuilder str = new StringBuilder();
		//List<ServiceTableLine> table = tabService;
		str.append("[");
		boolean addComa = false;
		for (ServiceTableLine line : tabService) {
			if(addComa)
				str.append(",");
			str.append("{");
			str.append(" service : '").append(line.getService()).append("'");
			str.append(", perCentille : "+new BigDecimal(line.getPerCentille()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", moyenneTempsExec : "+new BigDecimal(line.getMoyenneTempsExec()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", sumTpsExec : "+new BigDecimal(line.getSumTpsExec()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", pctNbAppelPerHour : "+new BigDecimal(line.getPctNbAppelPerHour()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", pctErreurClass : "+new BigDecimal(line.getPctErreurClass()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", pctErreurTechn : "+new BigDecimal(line.getPctErreurTechn()).setScale(2, BigDecimal.ROUND_CEILING));		
			str.append("}");
			addComa=true;
		}
		str.append("]");
		return str.toString();
	}
	
	public String getMethodTableJson(List<MethodTableLine> tabMethod) {
		StringBuilder str = new StringBuilder();
		str.append("["); 
		boolean addComa = false;
		for (MethodTableLine line : tabMethod) {
			if(addComa)
				str.append(",");
			str.append("{");
			str.append(" service : '").append(line.getService()).append("'");
			str.append(", methode : '").append(line.getMethode()).append("'");
			str.append(", perCentille : "+new BigDecimal(line.getPerCentille()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", moyenneTempsExec : "+new BigDecimal(line.getMoyenneTempsExec()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", sumTpsExec : "+new BigDecimal(line.getSumTpsExec()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", pctNbAppelPerHour : "+new BigDecimal(line.getPctNbAppelPerHour()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append(", pctErreurClass : "+new BigDecimal(line.getPctErreurClass()).setScale(2, BigDecimal.ROUND_CEILING));			
			str.append(", pctErreurTechn : "+new BigDecimal(line.getPctErreurTechn()).setScale(2, BigDecimal.ROUND_CEILING));
			str.append("}");
			addComa=true;
		}
		str.append("]");
		return str.toString();
	}
	
	public String getErrorsTableJson(List<ErrorsTableLine> tabErreurs) {
		if(tabErreurs.size()==0){
			System.out.println("TableBuilder.getErrorsTableJson: "
					+ "Can't build table since the List of rows are empty");
			return null;
			}
		StringBuilder str = new StringBuilder();
		str.append(" [ "); 		
		boolean addComa = false;
		for (ErrorsTableLine line : tabErreurs) {
			if(addComa)
				str.append(",");
			str.append("{");
			str.append(" service : '").append(line.getService()).append("'");
			str.append(", methode : '").append(line.getMethode()).append("'");
			str.append(", erreurCode : '").append(line.getErreurCode()).append("'");
			str.append(", pourcentage : '").append(new BigDecimal(line.getPourcentage()).setScale(2, BigDecimal.ROUND_CEILING)).append("'");
			str.append(", libelleErreur : '").append(line.getLibelleErreur()).append("'");
			str.append(" } ");
			addComa=true;
		}
		str.append("]");
		//System.out.println("Je error contient: "+str);
		return str.toString();
	}
}
